﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace JuCloudy.Sys.App.SystemVariable.controls
{
    public partial class Insert : UserControl
    {
        public Insert()
        {
            InitializeComponent();

            this.Loaded += (obj, e) =>
            {
                OnLoad();
            };
        }

        public JuCloudy.Sys.App.SystemVariable.MainPage Mainpage { get; set; }

        public JuCloudy.Data.Entity.SysVariable Entity { get; set; }

        public void OnLoad()
        {
            if (Entity != null)
            {
                this.txt_name.Text = Entity.Name;
                this.txt_defaultvalue.Text = Entity.DeafultValue;
                this.txt_value.Text = Entity.Value;
                this.select_cache.SelectedIndex = (int)Entity.Cache;
                this.select_level.SelectedIndex = (int)Entity.Level;
            }
        }

        private void btn_Submit_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(this.txt_defaultvalue.Text) &&
                String.IsNullOrEmpty(this.txt_name.Text) &&
                String.IsNullOrEmpty(this.txt_value.Text) &&
                this.select_cache.SelectedItem == null &&
                this.select_level.SelectedItem == null)
            {
                MessageBox.Show("表单不能为空！");
                return;
            }

            
            JuCloudy.Data.Entity.SysVariable entity = new Data.Entity.SysVariable();
            if (Entity != null)
            {
                entity.Id = Entity.Id;
                entity.LastValue = Entity.Value;
            }

            entity.Cache = this.select_cache.SelectedIndex;
            entity.Categories = 0;
            entity.DeafultValue = this.txt_defaultvalue.Text;
            entity.Level = this.select_level.SelectedIndex;
            entity.Name = this.txt_name.Text;
            entity.Value = this.txt_value.Text;
            entity.LastUpdataDate = DateTime.Now;
            entity.CreateDate = DateTime.Now;
            entity.LastUpdataDateTimestamp = 0;
            entity.CreateDateTimestamp = 0;
            entity.LastValue = String.Empty;
            Mainpage.insert(entity);
            //throw new Exception(entity.Serialize());
        }
    }
}
